package com.pachong.android.frameworkbase.customviews.iconcontent;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pachong.android.frameworkbase.R;
import com.pachong.android.frameworkbase.customviews.BadgeView;
import com.pachong.android.frameworkbase.utils.DimensionUtil;


/**
 * IconTextArrowButton
 * <p/>
 * 左边是个Icon, 可设置Visible
 * 接着是个Text,就当做是title
 * 再接着是容器, 用户可以自定义这一块, IconTextArrowButton默认往容器里填充了一个TextView, 用户可以设置gravity等属性
 * 右边是个icon, 可以设置具体图片, 不设置就默认为invisible
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: long long ago
 * @UpdateDate: 2016-06-28
 * 创建一个base class，并将icon的部分放到base中
 * @Version: 1.0.1
 */
public class IconTextArrowButton extends IconContentView {

    private static final int DEF_GRAVITY = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
    // 获取资源属性值时的无效值
    private static final int INVALIDE_VALUE = -1;

    private TextView mTvText = null;
    private BadgeView mBadgeView = null;


    public IconTextArrowButton(Context context) {
        this(context, null);
        setupViews(null);
    }


    public IconTextArrowButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupViews(attrs);
    }


    private void setupViews(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.IconTextArrowButton);

            // set text
            String labelStr = a.getString(R.styleable.IconTextArrowButton_android_text);
            float textSize = a.getDimension(R.styleable.IconTextArrowButton_android_textSize, INVALIDE_VALUE);
            int colorValue = a.getColor(R.styleable.IconTextArrowButton_android_textColor, INVALIDE_VALUE);
            setText(labelStr, textSize, colorValue);

            // sub text
            String subText = a.getString(R.styleable.IconTextArrowButton_subText);
            int subTxtColorValue = a.getColor(R.styleable.IconTextArrowButton_subTextColor, INVALIDE_VALUE);
            float subTxtSize = a.getDimension(R.styleable.IconTextArrowButton_subTextSize, INVALIDE_VALUE);
            float subPaddingRight = a.getDimension(R.styleable.IconTextArrowButton_subPaddingRight, INVALIDE_VALUE);
            int gravity = a.getInt(R.styleable.IconTextArrowButton_subTextGravity, DEF_GRAVITY);
            setSubText(subText, subTxtColorValue, subTxtSize, subPaddingRight, gravity);


            // set arrow view
            Drawable arrowDraw = a.getDrawable(R.styleable.IconTextArrowButton_arrow);
            if (arrowDraw != null) {
                ImageView ivArrow = new ImageView(getContext());
                ivArrow.setImageDrawable(arrowDraw);
                ivArrow.setPadding(0, 0, 0, 0);
                int arrowSize = (int) a.getDimension(R.styleable.IconTextArrowButton_arrowSize, INVALIDE_VALUE);
                if (arrowSize == INVALIDE_VALUE) {
                    arrowSize = DimensionUtil.dip2px(getContext(), 20);
                }

                LinearLayout.LayoutParams arrowParam = new LinearLayout.LayoutParams(arrowSize, LinearLayout.LayoutParams.WRAP_CONTENT);
                getSubSubContentLayout().addView(ivArrow, arrowParam);
            }

            //set mainView margin
            float mainMarginLeft = a.getDimension(R.styleable.IconTextArrowButton_mainMarginLeft, 0);
            float mainMarginRight = a.getDimension(R.styleable.IconTextArrowButton_mainMarginRight, 0);
            if (mainMarginLeft != 0 || mainMarginRight != 0) {
                LinearLayout mainContent = getMainContentLayout();
                LayoutParams params = (LayoutParams) mainContent.getLayoutParams();
                params.setMargins((int) mainMarginLeft, 0, (int) mainMarginRight, 0);
            }


            a.recycle();
        }

        mBadgeView = new BadgeView(getContext());
        mBadgeView.setTargetView(mTvText);

    }


    // region sub view API
    public void addSubView(View customView, LayoutParams params, int gravity) {
        getSubContentLayout().addView(customView, params);
        getSubContentLayout().setGravity(gravity);
    }


    public void addSubView(View customView, LayoutParams params) {
        addSubView(customView, params, DEF_GRAVITY);
    }


    public void addSubView(View customView, int gravity) {
        addSubView(customView, getDefParams(), gravity);
    }

    public void addSubView(View customView) {
        addSubView(customView, DEF_GRAVITY);
    }
    // endregion


    // region set sub text api
    private void setSubText(CharSequence subTxt, int subTxtColorValue, float subTxtSize, float subPaddingRight, int layoutGravity) {
        TextView tvSubView = getSubTextView();
        if (tvSubView == null) {
            getSubContentLayout().removeAllViews();
            tvSubView = new TextView(getContext());
            tvSubView.setGravity(Gravity.CENTER);
            LayoutParams txtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            txtParams.gravity = Gravity.CENTER_VERTICAL;
            tvSubView.setSingleLine();
            addSubView(tvSubView);
        }
        tvSubView.setText(subTxt);
        setSubTextViewGravity(Gravity.CENTER_VERTICAL | layoutGravity);

        if (subTxtColorValue != INVALIDE_VALUE) {
            tvSubView.setTextColor(subTxtColorValue);
        }
        if (subTxtSize != INVALIDE_VALUE) {
            tvSubView.setTextSize(TypedValue.COMPLEX_UNIT_PX, subTxtSize);
        }
        if (subPaddingRight != INVALIDE_VALUE) {
            int paddingLeft = DimensionUtil.dip2px(getContext(), 6);
            tvSubView.setPadding(paddingLeft, 0, (int) subPaddingRight, 0);
        }
        tvSubView.setVisibility(VISIBLE);
    }


    public void setSubText(CharSequence chars) {
        setSubText(chars, INVALIDE_VALUE, INVALIDE_VALUE, 0, getSubTextGravity());
    }


    public void setSubText(CharSequence chars, int gravity) {
        setSubText(chars, INVALIDE_VALUE, INVALIDE_VALUE, INVALIDE_VALUE, gravity);
    }

    public void setSubTextColor(int color) {
        TextView tv = getSubTextView();
        if (tv != null) {
            tv.setTextColor(color);
        }
    }


    public TextView getSubTextView() {
        TextView tvSubView = null;
        if (getSubContentLayout().getChildCount() > 0 && getSubContentLayout().getChildAt(0) instanceof TextView) {
            tvSubView = (TextView) getSubContentLayout().getChildAt(0);
        }
        return tvSubView;
    }


    public int getSubTextGravity() {
        TextView subTv = getSubTextView();
        if (subTv == null) {
            return DEF_GRAVITY;
        } else {
            return ((LayoutParams) subTv.getLayoutParams()).gravity;
        }
    }


    public void setSubTextViewGravity(int gravity) {
        if (getSubTextGravity() != gravity) {
            getSubContentLayout().setGravity(gravity);
        }
    }

    // endregion


    // region set text API
    public void setText(String text) {
        setText(text, INVALIDE_VALUE, INVALIDE_VALUE);
    }


    public void setText(int textId) {
        setText(getContext().getString(textId), INVALIDE_VALUE, INVALIDE_VALUE);
    }


    public String getText() {
        return mTvText == null ? null : mTvText.getText().toString();
    }


    /**
     * @param text
     * @param textSize  INVALIDE_VALUE为无效的值
     * @param textColor INVALIDE_VALUE为无效的值
     */
    public void setText(String text, float textSize, int textColor) {
        if (mTvText == null) {
            mTvText = new TextView(getContext());
            mTvText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            LayoutParams txtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            txtParams.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
            getMainContentLayout().addView(mTvText, txtParams);
        }

        if (!TextUtils.isEmpty(text)) {
            mTvText.setText(text);
        }

        if (textSize != INVALIDE_VALUE) {
            mTvText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }

        if (textColor != INVALIDE_VALUE) {
            mTvText.setTextColor(textColor);
        }
    }
    // endregion set text API


//	public void setBadge(String count) {
////		mBadgeView.setGravity(Gravity.TOP | Gravity.RIGHT);
////		mBadgeView.setBadgeCount(count);
////		mBadgeView.setText(count);
//	}


    public void setTextBadge(CharSequence contStr) {
        mBadgeView.setBadgeGravity(Gravity.RIGHT | Gravity.TOP);
        mBadgeView.setText(contStr);
        mBadgeView.setTextColor(Color.RED);
        mBadgeView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 6);
//        mBadgeView.setPadding();
        mBadgeView.setBackground(6, Color.RED);
    }


    public void setTextBadge(int count) {
        mBadgeView.setBadgeGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        mBadgeView.setTextColor(Color.RED);
        mBadgeView.setBadgeCount(count);
        mBadgeView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        mBadgeView.setBackground(0, Color.RED);
    }


}
